package run.microservice.stock.serializer;

import com.fasterxml.jackson.databind.BeanDescription;
import com.fasterxml.jackson.databind.SerializationConfig;
import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;
import com.fasterxml.jackson.databind.ser.BeanSerializerModifier;
import lombok.extern.slf4j.Slf4j;
import run.microservice.stock.annotations.Dict;
import java.util.List;

/**
 * 字典序列化修改类
 * @author lunfangyu
 */
@Slf4j
public class DictSerializerModifier extends BeanSerializerModifier {

    @Override
    public List<BeanPropertyWriter> changeProperties(SerializationConfig config, BeanDescription beanDesc, List<BeanPropertyWriter> beanPropertyWriters) {
        for (BeanPropertyWriter writer : beanPropertyWriters) {
            Dict dict = writer.getAnnotation(Dict.class);
            if (dict != null){
                DictSerializer dictSerializer = new DictSerializer(dict.value());
                writer.assignSerializer(dictSerializer);
            }
        }
        return beanPropertyWriters;
    }

}
